/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "onelinerxmlparser.h"

#include <QXmlStreamReader>
#include <QByteArray>
#include <QList>
#include <QDate>
#include <QTime>
#include <QDebug>

#include "../Types/message.h"

char readQueueEntry(QXmlStreamReader& xmlReader, OMessage& m)
{
    QXmlStreamReader::TokenType nodeType = xmlReader.tokenType();

    while ( nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "entry" )
    {
        if ( nodeType == QXmlStreamReader::StartElement )
        {
            if ( xmlReader.name() == "entry" )
            {
                QString messageDate = xmlReader.attributes().value("time").toString();
                m.date = QDate::fromString(messageDate.left(10),"yyyy-MM-dd");
                m.time = QTime::fromString(messageDate.remove(0,11),"hh:mm:ss");
            }
            else if ( xmlReader.name() == "author")
            {
                m.flag = xmlReader.attributes().value("flag").toString();

                nodeType = xmlReader.readNext();

                if ( nodeType == QXmlStreamReader::Characters )
                {
                    m.author = xmlReader.text().toString();
                }
                else
                {
                    qDebug() << "Some data were expected in author node, but missing!";
                }
            }
            else if ( xmlReader.name() == "message")
            {
                nodeType = xmlReader.readNext();

                if ( nodeType == QXmlStreamReader::Characters )
                {
                    m.message = xmlReader.text().toString();
                }
                else
                {
                    qDebug() << "Some data were expected in message node, but missing!";
                }
            }
            else
            {
                qDebug() << "Unknow node: " << xmlReader.name();
            }
        }

        nodeType = xmlReader.readNext();
    }

    return 0;
}

QList<OMessage> Parser :: readXMLOneliner(const QByteArray& onelinerData)
{
    QList<OMessage> oneliner;
    QXmlStreamReader xmlReader(onelinerData);
    QXmlStreamReader::TokenType nodeType;

    qDebug() << "Parser :: readXMLOneliner";

    while ( !xmlReader.atEnd() )
    {
        nodeType = xmlReader.readNext();

        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "oneliner" )
        {
            while(nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "oneliner")
            {
                if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "entry" )
                {
                    OMessage m;
                    readQueueEntry(xmlReader, m);
                    oneliner.push_back(m);
                }

                nodeType = xmlReader.readNext();
            }
        }
    }

    qDebug() << oneliner;

    return oneliner;
}
